package com.itheima.reggie.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.User;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录的安全校验
 */
@WebFilter("/*")
public class LoginFilter implements Filter {
    //Ant风格,路径匹配器,支持通配符
    public static final AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String[] loginUrls = {
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/common/**",
                "/user/sendMsg",
                "/user/login"
        };
        //Ant风格
       // AntPathMatcher antPathMatcher = new AntPathMatcher();
        HttpServletRequest request =  (HttpServletRequest)servletRequest;
        String requestURI = request.getRequestURI();
        for (String loginUrl : loginUrls) {
            if(antPathMatcher.match(loginUrl,requestURI)){
                //设置值
                Long empId = (Long) request.getSession().getAttribute("employee");
                BaseContext.setCurrentId(empId);


                filterChain.doFilter(servletRequest,servletResponse);
                return;
            }
        }

        Long employee = (Long) request.getSession().getAttribute("employee");
        if(employee != null){
            //设置值
            /*Employee  emp = (Employee) request.getSession().getAttribute("employee");
            Long empId = emp.getId();
            BaseContext.setCurrentId(empId);*/
            Long empId = (Long) request.getSession().getAttribute("employee");
            BaseContext.setCurrentId(empId);

            //放行
            filterChain.doFilter(servletRequest,servletResponse);

            //回来的代码,需要释放ThreadLocal中存储的用户id,避免内存溢出的风险
            BaseContext.release();
            return;
        }

        Long user = (Long) request.getSession().getAttribute("user");
        if(user != null){
            //设置值,登录成功,向ThreadLocal存入当前用户id
            Long useId = (Long) request.getSession().getAttribute("user");
            BaseContext.setCurrentId(useId);

            //放行
            filterChain.doFilter(servletRequest,servletResponse);

            //回来的代码,需要释放ThreadLocal中存储的用户id,避免内存溢出的风险
            BaseContext.release();
            return;
        }


        HttpServletResponse response =(HttpServletResponse)servletResponse;

        /*if(request.getSession().getAttribute("user") != null){
           // log.info("用户已登录，用户id为：{}",request.getSession().getAttribute("user"));

            Long userId = (Long) request.getSession().getAttribute("user");
           BaseContext.setCurrentId(userId);

            filterChain.doFilter(request,response);
            return;
        }*/
        R<Employee> nl = R.error("NOTLOGIN");
        response.getWriter().write(JSON.toJSONString(nl));

    }
}
